package com.mdhw.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdhw.common.annotation.IgnorePermissions;
import com.mdhw.common.base.PageData;
import com.mdhw.common.enums.StatusEnum;
import com.mdhw.config.R;
import com.mdhw.system.entity.SysDictData;
import com.mdhw.system.entity.SysDictType;
import com.mdhw.system.entity.SysMenu;
import com.mdhw.system.service.SysDictDataService;
import com.mdhw.system.service.SysDictTypeService;
import com.mdhw.system.vo.SysMenuVo;
import com.mdhw.system.vo.YonghuXieyiVo;
import com.mdhw.system.vo.ZxConfigVo;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Huang
 * @date 2022-03-21
 */
@Slf4j
@RestController
@Api(tags = "数据字典")
@RequestMapping("sysDict")
public class SysDictController {

    @Autowired
    private SysDictTypeService sysDictTypeService;
    @Autowired
    private SysDictDataService sysDictDataService;



    @GetMapping("/list")
    @ApiOperation(value = "数据字典type列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "页码", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "单页容量", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "dictName", value = "名称", paramType = "query", dataType = "String")
    })
    public R<PageData<SysDictType>> getList(Integer pageNo, Integer pageSize, String dictName) {
        LambdaQueryWrapper<SysDictType> query = new LambdaQueryWrapper<>();
        query.eq(SysDictType::getStatus, StatusEnum.OK.getCode());
        if(dictName != null){

            query.like(SysDictType::getDictName,dictName);
        }
        Page<SysDictType> page = new Page<>(pageNo, pageSize);
        this.sysDictTypeService.page(page,query);
        PageData<SysDictType> pageData = PageData.plusPageToPageData(page);
        return R.success(pageData);
    }




    @GetMapping("/data/list")
    @ApiOperation(value = "数据字典data列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "页码", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "单页容量", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "dictLabel", value = "名称", paramType = "query", dataType = "String")
    })
    public R<PageData<SysDictData>> getDataList(Integer pageNo, Integer pageSize, String dictLabel,String dictType) {
        QueryWrapper<SysDictData> query = new QueryWrapper<>();
        query.lambda().eq(SysDictData::getStatus, StatusEnum.OK.getCode());
        if(dictType != null){
            query.lambda().eq(SysDictData::getDictType,dictType);
        }
        if(dictLabel != null){
            query.lambda().like(SysDictData::getDictLabel,dictLabel);
        }
        Page<SysDictData> page = new Page<>(pageNo, pageSize);
        this.sysDictDataService.page(page,query);
        PageData<SysDictData> pageData = PageData.plusPageToPageData(page);
        return R.success(pageData);
    }


    @IgnorePermissions
    @GetMapping("/byType")
    @ApiOperation(value="根据字典类型获取字典值列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dictType", value = "字典类型", paramType = "query", dataType = "String"),
    })
    public R<List<SysDictData>> getDictDataListByType(String dictType) {
        List<SysDictData> list = sysDictDataService.listByDictType(dictType);
        return R.success(list);
    }


    @GetMapping("/byMultiType")
    @ApiOperation(value="根据多个字典类型获取字典值Map")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dictType", value = "多个字典类型逗号分割", paramType = "query", dataType = "String"),
    })
    public R<Map<String, List<SysDictData>>> getMultiDictDataListByType(String dictType) {
        Map<String, List<SysDictData>> list = sysDictDataService.multiDictDataListByType(dictType);
        return R.success(list);
    }

    @IgnorePermissions
    @GetMapping("/get/selectYonghuXieyi")
    public R<YonghuXieyiVo> selectYonghuXieyi(){
        return this.sysDictTypeService.selectYonghuXieyi();
    }

    @IgnorePermissions
    @GetMapping("/get/selectZxConfigVo")
    public R<ZxConfigVo> selectZxConfigVo(){
        ZxConfigVo zxConfigVo = new ZxConfigVo();
        zxConfigVo.setAppVersionCode("100");
        zxConfigVo.setDowloadUrl("");
        return R.success(zxConfigVo);
    }
}
